﻿using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;
using System;

namespace KiraSh.ResumeTracker
{
    public partial class frmEmailSettings : Form
    {
        private static frmEmailSettings instance = null;
        private XMLDealer xml = new XMLDealer();
        private List<EmailServer> serverList = new List<EmailServer>();
        private string path = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), CurrentContext.Profile.Name), "EmailServers.xml");

        public static frmEmailSettings ShowForm()
        {
            if (instance == null)
            {
                instance = new frmEmailSettings();
            }

            instance.Show();
            instance.BringToFront();

            return instance;

        }

        private frmEmailSettings()
        {
            InitializeComponent();
            this.Text = CurrentContext.Name;
        }

        private void frmEmailSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        private void frmEmailSettings_Load(object sender, System.EventArgs e)
        {
            PopulateCheckBox();
        }

        private void PopulateCheckBox()
        {
            // populate from local folder if the file exists or copy the file to local folder if doesn't
            if (!File.Exists(path))
            {
                File.Copy(Path.Combine(Directory.GetCurrentDirectory(), "EmailServers.xml"), path);
            }
            object o = xml.LoadXML(path, typeof(List<EmailServer>));
            if (o != null)
            {
                serverList = (List<EmailServer>)o;
            }

            foreach (EmailServer server in serverList)
            {
                this.bxName.Items.Add(server);
            }
        }

        private void bxName_SelectedIndexChanged(object sender, EventArgs e)
        {
            EmailServer server = (EmailServer)this.bxName.SelectedItem;
            this.bxServer.Text = server.Server;
            this.bxPort.Text = server.Port.ToString();
            this.bxPassword.Text = server.SMTPPassword;
            this.bxSMTPUsername.Text = server.SMTPUserName;
            this.bxSSL.Checked = server.SSL;
            this.bxEmail.Text = server.EmailFrom;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            EmailServer server = (EmailServer)this.bxName.SelectedItem;
            bool found = false;
            if (server != null)
            {
                foreach (EmailServer ser in serverList)
                {
                    if (server.Name == ser.Name)
                    {
                        // save the current profile
                        serverList.Remove(ser);
                        serverList.Add(saveServerDetails());
                        found = true;
                        break;
                    }
                }
            }
            else
            {
                server = new EmailServer();
                if (!found)
                {
                    serverList.Add(saveServerDetails());
                }
            }

            xml.WriteXML(path, typeof(List<EmailServer>), serverList);
        }

        private EmailServer saveServerDetails()
        {
            EmailServer server = new EmailServer();
            server.Name = this.bxName.Text;
            server.Port = int.Parse(this.bxPort.Text);
            server.Server = this.bxServer.Text;
            server.SSL = this.bxSSL.Checked;
            server.SMTPPassword = this.bxPassword.Text;
            server.SMTPUserName = this.bxSMTPUsername.Text;
            server.EmailFrom = this.bxEmail.Text;
            return server;
        }


    }
}
